package org.mx.dal.service.impl;

import java.lang.reflect.Field;

import javax.persistence.Column;
import javax.persistence.Table;

import org.mx.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ReflectionUtils;

/**
 * 描述： 基于Hibernate JdbcTemplate的批量数据处理实现类
 *
 * @author john peng Date time 2019/5/25 7:41 PM
 */
public class JdbcBatchAccessorImpl extends AbstractJdbcBatchAccessorImpl {
	/**
	 * 构造函数
	 * @param jdbcTemplate Spring JDBC模版
	 */
	@Autowired
	public JdbcBatchAccessorImpl(JdbcTemplate jdbcTemplate) {
		super(jdbcTemplate);
	}

	@Override
	protected String getTableName(Class<?> entityClass) {
		Table table = entityClass.getAnnotation(Table.class);
		String name = null;
		if (table != null) {
			name = table.name();
		}
		if (StringUtils.isBlank(name)) {
			return entityClass.getName();
		}
		return name;
	}

	@Override
	protected String getColumnName(Class<?> entityClass, String fieldName) {
		Field field = ReflectionUtils.findField(entityClass, fieldName);
		Column column = field.getAnnotation(Column.class);
		if (column != null && !StringUtils.isBlank(column.name())) {
			return column.name();
		}
		return fieldName;
	}
}
